package view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.administrator.text.R;

/**
 * Created by liuxiaobin on 2016/12/27
 *
 */
public class WeLinearLayout extends LinearLayout{
    private Context mContext;
    private RoundImageView roundImageView;
    private String mStringLeft,mStringIV,mIVUnread;
    private OnWeLinearItemClickListener mOnWeLinearItemClickListener;
    private TextView mTvright;
    public WeLinearLayout(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public WeLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttrs(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {
        View view = RelativeLayout.inflate(mContext, R.layout.layout_weinformation, null);
        //以下两句的顺序不能调换，要先addView，然后才能通过findViewById找到该TextView
        addView(view);
        TextView tvlefg = (TextView)view.findViewById(R.id.textview_weinformation_lefg);
        ImageView imageView = (ImageView)view.findViewById(R.id.image_weLinear_ruent);
        mTvright = (TextView)view.findViewById(R.id.textview_weinformation_right);
        roundImageView = (RoundImageView)view.findViewById(R.id.weinformation_unread);
        RelativeLayout linearlayout = (RelativeLayout)view.findViewById(R.id.linear_weinformation);
        tvlefg.setText(mStringLeft);
        if(mStringIV.equals("1")){
            imageView.setVisibility(GONE);
        }

        linearlayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnWeLinearItemClickListener.onItemClick();
            }
        });

    }

    /**
     * 设置右边的字体
     */
    public void setTextViewlefg(String string){
        mTvright.setText(string);
    }

    /**
     * 设置红点的显示状态
     *
     */
    public void setRoundIVState(boolean state){
        if(state){
           roundImageView.setVisibility(VISIBLE);
        }else {
            roundImageView.setVisibility(GONE);
        }
    }

    /**
     * get attrs
     */
    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.we_view_information);
        mStringLeft = ta.getString(R.styleable.we_view_information_stringText_we);
        mStringIV = ta.getString(R.styleable.we_view_information_stringiv_we);
        ta.recycle();
    }

    /**
     * 目前是事件的回掉
     * 定义接口 用于itme  的回调
     */
    public interface OnWeLinearItemClickListener {
        void onItemClick();
    }
    public void setOnWeLinearClickListener(OnWeLinearItemClickListener listener) {
        this.mOnWeLinearItemClickListener = listener;
    }

}
